package be.kuleuven.cs.mop.domain.model.impl;


import be.kuleuven.cs.mop.domain.model.TaskStatus;



public class TaskStateUnfinished extends TaskState {
	
	protected TaskStateUnfinished(final TaskImpl task) {
		super(task);
	}
	
	/**
	 * When time changes, check whether this state has become available/unavailable
	 */
	public void onTimeChanged(final Clock time) {
		super.setUnfinished();
		super.onTimeChanged(time);
	}
	
	
	@Override
	public void setUnfinishedSafely() {
		// Ignored
	}
	
	@Override
	public boolean isModifiable() { return true; }
	
	@Override
	public boolean isUnfinished() { return true; }
	
	/**
	 * Checks whether this task is available
	 * 
	 * @return true if the current time is covered by this task's schedule, all dependency tasks are finished succesfully, 
	 * the minimum of helper users is met, and all necessary resources are available
	 */
	@Override
	public boolean isAvailable() {
		if(!getTask().scheduleCoversCurrentTime())
			return false;
		
		for (final TaskImpl task : getTask().getDependencies())
			if (!task.getState().isSuccessful())
				return false;
		
		if(!getTask().satisfiesAcceptedHelperUserRequirements()){
			return false;
		}
				
		if(!getTask().satisfiesReservationRequirements()){
			return false;
		}
		return true;
	}
	
	@Override
	public TaskStatus toStatus() { return TaskStatus.UNFINISHED; }
	

	
}